<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
/**
 * 用户模型
 */
class Admin_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	
	/**
	 * 注册
	 */
	public function user_insert() {
		$data = array (
				'user_account' => $_POST ['user_account'],
				'up_account' => $_POST ['up_account'],
				'pwd' => md5 ( $_POST ['pwd'] ),
				'spwd' => md5 ( $_POST ['spwd'] ),
				'user_name' => $_POST ['user_name'],
				'mobile' => $_POST ['mobile'],
				'zfb_account' => $_POST ['zfb_account'],
				'bank_name' => $_POST ['bank_name'],
				'bank_card' => $_POST ['bank_card'],
				'wx_id' => $_POST ['wx_id'],
				'create_time' => time (),
				'permission' => 0/* $_POST ['permission'] */ 
		);
		
		$result = $this->db->insert ( 'pc_ddth_admin', $data );
		
		return $result;
	}
	
	/**
	 * 按账号查询用户
	 */
	public function get_uesr_info($user_account) {
		$sql = "SELECT * FROM pc_ddth_admin WHERE user_account = '$user_account'";
		
		$query = $this->db->query ( $sql );
		
		return $query->row_array ();
	}
	
	/**
	 * 按id获取用户信息
	 */
	public function get_user_info_id($id) {
		$sql = "SELECT * FROM pc_ddth_admin WHERE id = $id";
		
		$query = $this->db->query ( $sql );
		
		return $query->row_array ();
	}
	
	/**
	 * 按账号密码查询用户
	 */
	public function check_user($acc, $pwd) {
		$sql = "SELECT * FROM pc_ddth_admin WHERE user_account = '$acc' AND pwd = '$pwd'";
		
		$query = $this->db->query ( $sql );
		
		return $query->row_array ();
	}
	
	/**
	 * 按账号二级密码查询用户
	 */
	public function check_spwd($id, $spwd) {
		$sql = "SELECT * FROM pc_ddth_admin WHERE id = $id AND spwd = '$spwd'";
		
		$query = $this->db->query ( $sql );
		
		return $query->row_array ();
	}
	
	/**
	 * 按id更新用户信息
	 */
	public function update_userinfo($id) {
		$data = array (
				'user_name' => $_POST ['user_name'],
				'mobile' => $_POST ['mobile'],
				'zfb_account' => $_POST ['zfb_account'],
				'bank_name' => $_POST ['bank_name'],
				'bank_card' => $_POST ['bank_card'],
				'wx_id' => $_POST ['wx_id'] 
		);
		
		$result = $this->db->update ( 'pc_ddth_admin', $data, array (
				'id' => $id 
		) );
		
		return $result;
	}
	
	/**
	 * 密码修改
	 * type 1 一级密码修改
	 * type 2 二级密码修改
	 */
	public function update_pwd($id, $pwd, $type) {
		if ($type == 1) {
			$data ['pwd'] = $pwd;
		} elseif ($type == 2) {
			$data ['spwd'] = $pwd;
		} else {
			return FALSE;
		}
		
		$result = $this->db->update ( 'pc_ddth_admin', $data, array (
				'id' => $id 
		) );
		
		return $result;
	}
}